﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for Friend
    /// </summary>
    public class Friend
    {
        private int friend_id;

        public int Friend_id
        {
            get
            {
                return friend_id;
            }
            set
            {
                friend_id = value;
            }
        }

        private int friend_list_id;

        public int Friend_list_id
        {
            get
            {
                return friend_list_id;
            }
            set
            {
                friend_list_id = value;
            }
        }

        private int friend_user_id;

        public int Friend_user_id
        {
            get
            {
                return friend_user_id;
            }
            set
            {
                friend_user_id = value;
            }
        }

        private bool is_subscriber;

        public bool Is_subscriber
        {
            get
            {
                return is_subscriber;
            }
            set
            {
                is_subscriber = value;
            }
        }

        private DateTime created_date;

        public DateTime Created_date
        {
            get
            {
                return created_date;
            }
            set
            {
                created_date = value;
            }
        }

        public Friend()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Friend(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                friend_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    friend_list_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    friend_user_id = sdr.GetInt32(2);
                if (!sdr.IsDBNull(3))
                    is_subscriber = sdr.GetBoolean(3);
                if (!sdr.IsDBNull(4))
                    created_date = sdr.GetDateTime(4);
            }
        }

        public static DataTable GetAll()
        {
            DBHelp dbh = new DBHelp();
            DataTable dt = new DataTable();
            string cmd = "Friend_GetAll";
            dt = dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static DataTable GetAll(int FriendListId)
        {
            DBHelp dbh = new DBHelp();
            DataTable dt = new DataTable();
            dbh.AddParameter("@friend_list_id", FriendListId);
            String cmd = "select * from tbl_friend where friend_list_id = @friend_list_id";
            dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
            return dt;
        }

        public static int GetFriendID(int FriendListID, int FriendUserID)
        {
            DBHelp dbh = new DBHelp();
            DataTable dt = new DataTable();
            dbh.AddParameter("@friend_list_id", FriendListID);
            dbh.AddParameter("@friend_user_id", FriendUserID);
            String cmd = "select * from tbl_friend where friend_list_id = @friend_list_id and friend_user_id = @friend_user_id";

            dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
            if (dt.Rows.Count == 0)
            {
                return -1;
            }
            else
            {
                return (int)dt.Rows[0]["friend_id"];
            }

        }

        public static DataTable GetByFriendListID(int FriendListID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@friend_list_id", FriendListID);
            DataTable dt = new DataTable();
            string cmd = "select * from tbl_friend where friend_list_id = @friend_list_id";
            dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
            return dt;
        }

        //Ham lay ve ID nhan vao
        public static Friend GetInfo(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@friend_id", ID);
            Friend friend = new Friend();
            string cmd = "Friend_GetInfo";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (sdr.Read())
            {
                friend.Friend_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    friend.Friend_list_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    friend.Friend_user_id = sdr.GetInt32(2);
                if (!sdr.IsDBNull(3))
                    friend.Is_subscriber = sdr.GetBoolean(3);
                if (!sdr.IsDBNull(4))
                    friend.Created_date = sdr.GetDateTime(4);
            }
            return friend;
        }

        //Them friend
        public static void Insert(Friend friend)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@friend_list_id", friend.Friend_list_id);
            dbh.AddParameter("@friend_user_id", friend.Friend_user_id);
            dbh.AddParameter("@is_subscriber", friend.Is_subscriber);
            dbh.AddParameter("@created_date", friend.Created_date);
            string cmd = "Friend_Insert";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        //Sua friend
        public static void Update(Friend friend)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@friend_id", friend.Friend_id);
            dbh.AddParameter("@friend_list_id", friend.Friend_list_id);
            dbh.AddParameter("@friend_user_id", friend.Friend_user_id);
            dbh.AddParameter("@is_subscriber", friend.Is_subscriber);
            dbh.AddParameter("@created_date", friend.Created_date);
            string cmd = "Friend_Update";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        //Xoa friend
        public static void Delete(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@friend_id", ID);
            string cmd = "Friend_Delete";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    }
}